<?php
// phpinfo();

function f_conectarse(){
/* conecta a la base de datos */		
	$is_driver = "mysql";
	$is_servidor = "localhost";
	$is_basededatos = "floresyfrutas";
	$is_usuario = "root";
	$is_contrasena = "";

	try {
		$dbh = new PDO("".$is_driver.":host=".$is_servidor.";dbname=".$is_basededatos."", $is_usuario, $is_contrasena );;
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	return $dbh;

}

function f_select($is_consulta, $ii_fetch = 0 ){ 
/* 	$ii_fetch = 0 -> fetch
	$ii_fetch = 1 -> fetchAll */

	try {
		$dbh = f_conectarse();
		
		$stmt = $dbh->prepare($is_consulta);
		if ($stmt->execute()) {
			if ( 0 == $ii_fetch){
				$row = $stmt->fetch();
			}else
			if ( 1 == $ii_fetch){
				$row = $stmt->fetchAll();
			}
		}
		$dbh = null;
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	
	return $row;
}


// $is_consulta_comuna = "SELECT * FROM comuna inner join provincia where comu_c_provincia = prov_codigo and comu_codigo = 05501";
// $ia_comuna = f_select( $is_consulta_comuna , 0 );
// print_r( $ia_comuna );
// echo "<br>";
// echo "<br>";
// echo "<br>";
// echo "<br>";

// $ia_comuna = f_select( $is_consulta_comuna , 1 );
// print_r( $ia_comuna );
// echo "<br>";
// echo "<br>";
// echo "<br>";
// echo "<br>";


// $is_consulta_comuna = "SELECT comu_codigo, comu_detalle, prov_codigo, prov_detalle FROM comuna inner join provincia where comu_c_provincia = prov_codigo";
// $ia_comuna = f_select( $is_consulta_comuna , 1 );
// print_r( $ia_comuna );

		
$dbh = f_conectarse();
	// try {
		/* 
$is_insertar_comuna = "INSERT INTO comuna (  comu_codigo, comu_detalle, comu_c_provincia, comu_vigente, comu_r_fecha_creacion ) 
						VALUES ( '06663', 'manuelGracia', '055', 1, now() ); ";
 *//* 
$is_insertar_comuna = "INSERT INTO  `floresyfrutas`.`accion` (
							`acc_codigo` ,
							`acc_descripcion` ,
							`acc_vigente` ,
							`acc_r_fecha_creacion` ,
							`acc_r_fecha_modificacion` ,
							`acc_r_usuario`
						) VALUES (
							NULL ,
							'prueba5',
							'1', 
							CURRENT_TIMESTAMP ,
							NULL ,
							'Rmunoz'
						); ";

		// $dbh = f_conectarse();
		// print_r($dbh);
		$stmt = $dbh->prepare($is_insertar_comuna);
		$stmt->execute();
		echo "<br>____________________________________";
		
		echo "<br>lastInsertId ";
		echo $lastId = $dbh->lastInsertId();
		echo "<br>____________________________________";
	
		$is_last_insert_id = 'SELECT LAST_INSERT_ID() as last_id';
		$stmt2 = $dbh->prepare($is_last_insert_id);
		$stmt2->execute();       
		$last_id = $stmt2->fetchAll();
		echo "<br>lastInsertId ";	
		echo $last_id = intval($last_id[0]['last_id']);

		echo "<br>____________________________________";
	
		$is_last_insert_id = 'SELECT count(comu_codigo) + 1 as last_id from comuna ';
		$stmt2 = $dbh->prepare($is_last_insert_id);
		$stmt2->execute();       
		$last_id = $stmt2->fetchAll();
		echo "<br>lastInsertId ";	
		echo $last_id = intval($last_id[0]['last_id']);

		echo "<br>____________________________________";
		echo "<br>rowCount ";
		echo $count = $stmt->rowCount();
		
		// $dbh = null;
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	} */
	/* 
	try {  
  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  $dbh->beginTransaction();
  $is_insertar_comuna = "INSERT INTO  `floresyfrutas`.`accion` (
							`acc_codigo` ,
							`acc_descripcion` ,
							`acc_vigente` ,
							`acc_r_fecha_creacion` ,
							`acc_r_fecha_modificacion` ,
							`acc_r_usuario`
						) VALUES (
							NULL ,
							'prueba5',
							'1', 
							CURRENT_TIMESTAMP ,
							NULL ,
							'Rmunoz'
						); ";

	// $dbh = f_conectarse();
	// print_r($dbh);
	$stmt = $dbh->prepare($is_insertar_comuna);
	$stmt->execute();
  
  $dbh->commit();
  
} catch (Exception $e) {
  $dbh->rollBack();
  echo "Failed: " . $e->getMessage();
}

 */
	 
 
/* 
function PDOBindArray(&$poStatement, &$paArray){
 
  foreach ($paArray as $k=>$v){

    @$poStatement->bindValue(':'.$k,$v);

  } // foreach
  
 } // function

// example

$stmt = $dbh->prepare("INSERT INTO tExample (id,value) VALUES (:id,:value)");

$taValues = array(
 'id' => '1',
 'value' => '2'
); // array

PDOBindArray($stmt,$taValues);

$stmt->execute();
  */
 
function PDOBindArray(&$poStatement, &$paArray){ 
    foreach ($paArray as $k=>$v) {
        @$poStatement->bindValue($k, $v[0], $v[1]);
    }      
}
try{
// the array structure should now look something like this
	$email = "rmunoz38";
	$pass = "testDBO";
	$is_insert_accion = "INSERT INTO  `floresyfrutas`.`accion` (
								`acc_codigo` ,
								`acc_descripcion` ,
								`acc_vigente` ,
								`acc_r_fecha_creacion` , 
								`acc_r_usuario`
							) VALUES (
								'NULL' ,
								:email,
								'1', 
								CURRENT_TIMESTAMP , 
								:pass
							); ";
	echo $is_insert_accion;
					
	$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

	$dbh->beginTransaction();	
	$stmt = $dbh->prepare($is_insert_accion);

	$inputArray = array(
		':email' => array($email, PDO::PARAM_STR), 
		':pass' => array($pass, PDO::PARAM_STR)
	);

	// var_dump(  $inputArray );
	PDOBindArray($stmt,$inputArray);

	// $stmt->debugDumpParams();
	$stmt->execute();
	$dbh->commit();

} catch (Exception $e) {
  $dbh->rollBack();
  echo "Failed: " . $e->getMessage();
}


	try {
		/* 
$is_insertar_comuna = "INSERT INTO comuna (  comu_codigo, comu_detalle, comu_c_provincia, comu_vigente, comu_r_fecha_creacion ) 
						VALUES ( '06663', 'manuelGracia', '055', 1, now() ); ";
 */
/* $is_insertar_comuna = "INSERT INTO  `floresyfrutas`.`accion` (
							`acc_codigo` ,
							`acc_descripcion` ,
							`acc_vigente` ,
							`acc_r_fecha_creacion` ,
							`acc_r_fecha_modificacion` ,
							`acc_r_usuario`
						) VALUES (
							NULL ,
							'prueba5',
							'1', 
							CURRENT_TIMESTAMP ,
							NULL ,
							'Rmunoz'
						); ";

		// $dbh = f_conectarse();
		// print_r($dbh);
		$stmt = $dbh->prepare($is_insertar_comuna);
		$stmt->execute();
		echo "<br>____________________________________"; */
		
		echo "<br>lastInsertId ";
		echo $lastId = $dbh->lastInsertId();
		echo "<br>____________________________________";
	
		$is_last_insert_id = 'SELECT LAST_INSERT_ID() as last_id';
		$stmt2 = $dbh->prepare($is_last_insert_id);
		$stmt2->execute();       
		$last_id = $stmt2->fetchAll();
		echo "<br>lastInsertId ";	
		echo $last_id = intval($last_id[0]['last_id']);

		echo "<br>____________________________________";
	
		$is_last_insert_id = 'SELECT count(acc_codigo) + 1 as last_id from accion ';
		$stmt2 = $dbh->prepare($is_last_insert_id);
		$stmt2->execute();       
		$last_id = $stmt2->fetchAll();
		echo "<br>count+1: ";	
		echo $last_id = intval($last_id[0]['last_id']);

		echo "<br>____________________________________";
		echo "<br>rowCount ";
		echo $count = $stmt->rowCount();
		
		// $dbh = null;
	} catch (PDOException $e) {
		print "Error!: " . $e->getMessage() . "<br/>";
		die();
	}
	
?>
 